package com.kefu.call.vo;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;
import com.kefu.call.model.ActivityInventoryInfo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.util.StreamUtil;
import com.kefu.common.vo.AbstractInsertVo;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * @author francis
 * @date 2021/05/22
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@ApiModel(description = "活动清单")
public class ActivityInventoryInfoInsertBatchVo extends AbstractInsertVo<List<ActivityInventoryInfo>> {

	@NotEmpty
    @Size(max = 32)
    @ApiModelProperty(value = "活动ID")
    private String activityId;
	@NotNull
    @Size(max = 50)
    @ApiModelProperty(value = "活动名称")
    private String activityName;
    @ApiModelProperty(value = "数据状态0 无效，1有效",hidden=true)
    private Integer dataStatus=1;
    
    @ApiModelProperty(value = "清单列表")
    private List<ActivityInventoryVo> list;

    @Data
    public static class ActivityInventoryVo{
    	@Size(max = 32)
    	@ApiModelProperty(value = "所属用户")
    	private String userId;
    	@Size(max = 32)
    	@ApiModelProperty(value = "所属用户")
    	private String userName;
    	@ApiModelProperty(value = "清单状态",example="1",allowableValues="1,2,3,4，5，6",notes="1 未分配，2已分配，3处理中，4预约，5处理完成，6 处理失败")
    	private Integer inventoryStatus;
    	
    	@Size(max = 32)
    	@ApiModelProperty(value = "客户ID")
    	private String customerId;
    	
    	@NotEmpty
    	@Size(max = 32)
    	@ApiModelProperty(value = "客户名称")
    	private String customerName;
    	
	    @ApiModelProperty(value = "客户号码",notes="客户ID和客户号码两者必须有一个，否则该清单无法处理")
	    private String customerPhone;
    }


    @Override
    public List<ActivityInventoryInfo>convert() {
    	List<ActivityInventoryInfo> list=Lists.newArrayList();
    	KefuContext context = KefuContext.getKefuContext();
    	AtomicLong now=new AtomicLong(context.getNow());
    	StreamUtil.stream(this.list)
    		.forEach(inventory->{
    			now.getAndIncrement();
    			ActivityInventoryInfo info = new ActivityInventoryInfo();
    			info.setActivityId(activityId);
    			info.setInventoryName(String.format("%s-%s",activityName,inventory.customerName));
    			info.setDataStatus(dataStatus);
    			info.setUserId(inventory.userId);
    			info.setUserName(inventory.userName);
    			info.setInventoryStatus(inventory.inventoryStatus);
    			int assignmentStatus=1;
    			if(StringUtils.hasLength(info.getUserId())) {
    				assignmentStatus=2;
    			}
    			info.setAssignmentStatus(assignmentStatus);
    			info.setIntentionStatus(0);
    			info.setCustomerId(inventory.customerId);
    			info.setCustomerName(inventory.customerName);
    			info.setCustomerPhone(inventory.customerPhone);
    			info.setInventoryId(IdUtil.simpleUUID());
    			info.setCreateTime(now.get());
    			info.setCreateUser(context.getUserId());
    			info.setUpdateTime(now.get());
    			info.setUpdateUser(context.getUserId());
    			info.setTenantId(context.getTenantId());
    			list.add(info);
    		});
	    return list;
	}
}